<template>
  <div>
    <!-- 自动派单 2018/6/14 ZuoW -->
    <Row>
      <i-col>
        <Button type="primary" @click="autoDispatchOrderAdd">添加</Button>
      </i-col>
    </Row>

    <Row style="margin-top: 20px;">
      <i-col>
        <tablePage ref="inspectionTab" :api="getAutoDispatcherList" :param="tabParam" :isStartLoad="true">
          <template slot-scope="props">
            <Table :columns="inspectionColumn" :data="props.data" @on-sort-change="props.sortChange" border></Table>
          </template>
        </tablePage>
      </i-col>
    </Row>

    <xlinkModal v-model="operatModal.autoDispatchOrderAddShow" className="vertical-center-modal" title="添加自动派单" width="700">
      <autoDispatchOrderAdd v-if="operatModal.autoDispatchOrderAddShow" @close="AddtableReset"></autoDispatchOrderAdd>
    </xlinkModal>
    <xlinkModal v-model="operatModal.autoDispatchOrderEditShow" className="vertical-center-modal" title="编辑自动派单" width="700">
      <autoDispatchOrderEdit v-if="operatModal.autoDispatchOrderEditShow" @close="EdittableReset" :currentOrder="orderCurrentRow"></autoDispatchOrderEdit>
    </xlinkModal>
  </div>
</template>

<script>
  import tablePage from '@components/table/tablePage.vue'
  import autoDispatchOrderAdd from './autoDispatchOrderAdd.vue'
  import autoDispatchOrderEdit from './autoDispatchOrderEdit.vue'
  import {getAutoDispatcherList, editAutoDispatcher, detectAutoDispatcherConfig, deleteAutoDispatcher} from '@api/backEnd/mainOrder/index'
  import xlinkModal from '@components/modal/xlinkModal'
  import moment from 'moment'

  export default {
    name: 'dispatchToFleet',
    data () {
      let operatModal = {
        autoDispatchOrderAddShow: false,
        autoDispatchOrderEditShow: false
      }
      let orderCurrentRow = {}
      let tabParam = {}
      let inspectionColumn = [
        {
          title: '派单状态',
          key: 'dispatchStatus',
          width: 100,
          render: (h, params) => {
            let thisData = ''
            if (+params.row.dispatchStatus === 1) {
              thisData = (<span style="color:green;">启用</span>)
            } else if (+params.row.dispatchStatus === 2) {
              thisData = (<span style="color:red;">禁用</span>)
            }
            return (thisData)
          }
        },
        {
          title: '派单方式',
          key: 'dispatchType',
          width: 180,
          render: (h, params) => {
            let thisData = ''
            if (+params.row.dispatchType === 1) {
              thisData = '整列派单'
            } else if (+params.row.dispatchType === 2) {
              thisData = '按照收货人派单'
            }
            return (<span>{thisData}</span>)
          }
        },
        {
          title: '派单频次',
          key: 'dispatchFrequency',
          width: 100,
          render: (h, params) => {
            let thisData = ''
            if (+params.row.dispatchFrequency === 1) {
              thisData = '仅此一次'
            } else if (+params.row.dispatchFrequency === 2) {
              thisData = '每天'
            }
            return (<span>{thisData}</span>)
          }
        },
        {
          title: '开始时间',
          width: 200,
          render: (h, params) => {
            let thisdata = params.row.beginTime
            thisdata = thisdata ? moment(thisdata).format('YYYY-MM-DD HH:mm') : ''
            return (<span>{thisdata}</span>)
          }
        },
        {
          title: '结束时间',
          width: 200,
          render: (h, params) => {
            let thisdata = params.row.endTime
            thisdata = thisdata ? moment(thisdata).format('YYYY-MM-DD HH:mm') : ''
            return (<span>{thisdata}</span>)
          }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          minWidth: 180,
          render: (h, params) => {
            let editBtn = (<i-button type="primary" size="small" style="margin-right: 5px;" onClick={() => this.autoDispatchOrderEdit(params.row)}>编辑</i-button>)
            let otherBtn = ''
            if (+params.row.dispatchStatus === 2) {
              otherBtn = (<i-button type="success" size="small" style="margin-right: 5px;" onClick={() => this.autoDispatchOrderEnable(params.row)}>启用</i-button>)
            } else if (+params.row.dispatchStatus === 1) {
              otherBtn = (<i-button type="warning" size="small" style="margin-right: 5px;" onClick={() => this.autoDispatchOrderDisable(params.row)}>禁用</i-button>)
            }
            let deleteBtn = (<i-button type="error" size="small" style="margin-right: 5px;" onClick={() => this.autoDispatchOrderDelete(params.row)}>删除</i-button>)
            let btnArray = []
            btnArray.push(editBtn)
            btnArray.push(otherBtn)
            btnArray.push(deleteBtn)
            return (btnArray)
          }
        }
      ]
      let datas = {getAutoDispatcherList, operatModal, inspectionColumn, tabParam, orderCurrentRow}
      return datas
    },
    methods: {
      AddtableReset () {
        this.operatModal.autoDispatchOrderAddShow = false
        this.$refs['inspectionTab'].dataRefresh()
      },
      EdittableReset () {
        this.operatModal.autoDispatchOrderEditShow = false
        this.$refs['inspectionTab'].dataRefresh()
      },
      autoDispatchOrderAdd () {
        this.operatModal.autoDispatchOrderAddShow = true
      },
      autoDispatchOrderEdit (Row) {
        this.orderCurrentRow = Row
        this.operatModal.autoDispatchOrderEditShow = true
      },
      autoDispatchOrderEnable (Row) {
        this.$Modal.confirm({
          title: '启用',
          content: '确定启用？',
          onOk: () => {
            detectAutoDispatcherConfig().then(res => {
              if (+res.data.isSuccess === 1) {
                let param = {
                  'id': Row.id,
                  'userId': Row.userId,
                  'beginTime': Row.beginTime,
                  'endTime': Row.endTime,
                  'dispatchFrequency': Row.dispatchFrequency,
                  'dispatchType': Row.dispatchType,
                  'userType': Row.userType,
                  'dispatchStatus': '1'
                }
                editAutoDispatcher(param).then(res => {
                  if (+res.data.isSuccess === 1) {
                    this.$refs['inspectionTab'].dataRefresh()
                    this.$Notice.success({
                      title: '成功',
                      desc: '禁用成功！'
                    })
                  } else {
                    this.$Notice.warning({
                      title: '失败',
                      desc: res.data.res
                    })
                  }
                })
              } else {
                this.$Notice.warning({
                  title: '失败',
                  desc: res.data.res
                })
              }
            })
          },
          onCancel: () => {
          }
        })
      },
      autoDispatchOrderDisable (Row) {
        this.$Modal.confirm({
          title: '禁用',
          content: '确定禁用？',
          onOk: () => {
            let param = {
              'id': Row.id,
              'userId': Row.userId,
              'beginTime': Row.beginTime,
              'endTime': Row.endTime,
              'dispatchFrequency': Row.dispatchFrequency,
              'dispatchType': Row.dispatchType,
              'userType': Row.userType,
              'dispatchStatus': '2'
            }
            console.log('param', param)
            editAutoDispatcher(param).then(res => {
              if (+res.data.isSuccess === 1) {
                this.$refs['inspectionTab'].dataRefresh()
                this.$Notice.success({
                  title: '成功',
                  desc: '禁用成功！'
                })
              } else {
                this.$Notice.warning({
                  title: '失败',
                  desc: res.data.res
                })
              }
            })
          },
          onCancel: () => {
          }
        })
      },
      autoDispatchOrderDelete (Row) {
        if (+Row.dispatchStatus === 1) {
          this.$Modal.confirm({
            title: '删除',
            content: '该条自动派单正在启用中，确认要删除？',
            onOk: () => {
              let param = {
                'id': Row.id
              }
              deleteAutoDispatcher(param).then(res => {
                if (+res.data.isSuccess === 1) {
                  this.$refs['inspectionTab'].dataRefresh()
                  this.$Notice.success({
                    title: '成功',
                    desc: '删除成功！'
                  })
                } else {
                  this.$Notice.warning({
                    title: '失败',
                    desc: res.data.res
                  })
                }
              })
            },
            onCancel: () => {
            }
          })
        } else if (+Row.dispatchStatus === 2) {
          this.$Modal.confirm({
            title: '删除',
            content: '确认要删除该条内容？',
            onOk: () => {
              let param = {
                'id': Row.id
              }
              deleteAutoDispatcher(param).then(res => {
                if (+res.data.isSuccess === 1) {
                  this.$refs['inspectionTab'].dataRefresh()
                  this.$Notice.success({
                    title: '成功',
                    desc: '删除成功！'
                  })
                } else {
                  this.$Notice.warning({
                    title: '失败',
                    desc: res.data.res
                  })
                }
              })
            },
            onCancel: () => {
            }
          })
        }
      }
    },
    created () {
    },
    mounted () {
    },
    watch: {},
    computed: {},
    components: {tablePage, autoDispatchOrderAdd, xlinkModal, autoDispatchOrderEdit}
  }
</script>

<style scoped>

</style>
